/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.purpose.service.api.SafetyInvestmentService;
import com.koron.purpose.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "safetyInvestment", description = "安全投入统计查询")
@RestController
@RequestMapping("/safetyInvestment")
public class SafetyInvestmentController {
    private final static  Logger logger = LoggerFactory.getLogger(SafetyInvestmentController.class);

    @Autowired
    private SafetyInvestmentService safetyInvestmentService;

    @ApiOperation("安全投入统计查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "year", value = "年份", required = true, paramType = "query", dataType = "String"),
            /*@ApiImplicitParam(name = "manager", value = "单位code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "serialNumber", value = "编号",  paramType = "query", dataType = "String"),*/
            @ApiImplicitParam(name = "contributoryProject", value = "投入项目code",  paramType = "query", dataType = "String"),
    })
    @PostMapping("/statisticalQuery.htm")
    public MessageBean statisticalQuery(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String corpCode, @ApiIgnore Integer year, @ApiIgnore String manager,
                                        @ApiIgnore String serialNumber, @ApiIgnore String contributoryProject) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return safetyInvestmentService.statisticalQuery(corpCode, year, null, null, contributoryProject);
    }

    @ApiOperation("导出安全投入统计查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "水司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "year", value = "年份", required = true, paramType = "query", dataType = "String"),
            /*@ApiImplicitParam(name = "manager", value = "单位code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "serialNumber", value = "编号",  paramType = "query", dataType = "String"),*/
            @ApiImplicitParam(name = "contributoryProject", value = "投入项目code",  paramType = "query", dataType = "String"),
    })
    @GetMapping(value = "/export.htm")
    @Log(title = "导出安全投入统计查询", businessModule = "安全投入", businessManipulation = BusinessManipulation.EXPORT)
    public HttpEntity<?> export(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String corpCode, @ApiIgnore Integer year, @ApiIgnore String manager,
                                @ApiIgnore String serialNumber, @ApiIgnore String contributoryProject) {
        if (null == user) {
            return null;
        }
        List<Map> list = safetyInvestmentService.exportQuery(corpCode, year, null, null, contributoryProject);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        File file = new File("static/excel/SafetyInvestment.xlsx");
        try (
                InputStream inputStream = getClass().getClassLoader().getResourceAsStream("static/excel/SafetyInvestment.xlsx");
        ) {
            FileUtils.copyInputStreamToFile(inputStream, file);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("安全投入统计查询", file, "SafetyInvestment.btl", map);
    }
}
